#!/bin/bash

# Set up the build environment for building Drawpile appimages.
# Run inside a CentOS 6 docker image.
# This script is partially based on the scripts at
# https://github.com/probonopd/AppImages/blob/master/recipes/

set -e
set -x
. /opt/rh/devtoolset-3/enable

# Libsodium repo version is ancient
cd /
wget -O libsodium.tar.gz https://download.libsodium.org/libsodium/releases/libsodium-1.0.16.tar.gz
tar xfz libsodium.tar.gz
cd libsodium*/
./configure --prefix=/usr
make
make install

# Libmicrohttpd repo version is ancient
cd /
wget -O libmicrohttpd.tar.gz https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-0.9.59.tar.gz
tar xfz libmicrohttpd.tar.gz
cd libmicrohttpd*/
./configure --prefix=/usr --without-gnutls
make
make install

# Note: KDE Frameworks 5.36 is the last one that supports Qt 5.6
# Non-repo deps: ECM (needed to build KF5 libs)
cd /
wget -O ecm.tar.xz http://download.kde.org/stable/frameworks/5.36/extra-cmake-modules-5.36.0.tar.xz
tar xfJ ecm.tar.xz
cd extra-cmake-modules*/
cmake3 . -DCMAKE_INSTALL_PREFIX=/usr
make install

# Non-repo deps: Get and build KArchive
cd /
wget -O karchive.tar.xz http://download.kde.org/stable/frameworks/5.36/karchive-5.36.0.tar.xz
tar xfJ karchive.tar.xz
cd karchive*/
mkdir build
cd build
cmake3 .. -DCMAKE_INSTALL_PREFIX=/usr
make
make install


# Non-repo deps: Get and build KDNSSD
# TODO

# Non-repo tools: AppimageKit
cd /
wget -O appimagekit.zip https://github.com/probonopd/AppImageKit/archive/master.zip
unzip appimagekit.zip
APPIMGKIT=/AppImageKit-master
cd $APPIMGKIT

cmake3 .
make

# Finalize
ldconfig

